<?php
/**
 * Created by PhpStorm.
 * User: JSQ-SL
 * Date: 2018/10/19
 * Time: 16:16
 */

namespace app\index\table;

use app\index\controller\Login;
use app\index\service\ExcelService;
use think\Db;

class ConstructionTable extends BaseTable
{
    protected static $view = 'V_CONSTRUCTION';
    protected static $table = 'CONSTRUCTION_INFO';
    protected static $pk = 'CONS_ID';
    protected static $configTable = 'CONSTRUCTION_CONFIG';

    static public function table(){
        return Db::table(static::$view);
    }

    static public function add($data){
        $data['INPUT_USER_ID'] = Login::getUserId();
        $data['INPUT_TIME'] = date('Y-m-d');

        self::toDate($data,'START_DATE');
        self::toDate($data,'PLAN_COMPLETE_DATE');
        self::toDate($data,'USE_DATE');
        self::toDate($data,'INPUT_TIME');
        Db::table(static::$table)->insert($data);
    }

    static public function addImport($data){
        $data['INPUT_USER_ID'] = Login::getUserId();
        $data['INPUT_TIME'] = date('Y-m-d');

        self::toDate($data,'START_DATE');
        self::toDate($data,'PLAN_COMPLETE_DATE');
        self::toDate($data,'USE_DATE');
        self::toDate($data,'INPUT_TIME');
        Db::table(static::$table)->insert($data);
    }

    static public function update($where,$data){
        self::toDate($data,'START_DATE');
        self::toDate($data,'PLAN_COMPLETE_DATE');
        self::toDate($data,'USE_DATE');
        Db::table(static::$table)->where($where)->update($data);
    }

    static public function delete($where){
        return Db::table(static::$table)->where($where)->delete();
    }

    public static function getConfigAdd(){
        return Db::table(static::$configTable)->where(['ADD_FLAG'=>'1'])->order('ID ASC')->select();
    }

    public static function getConfigEdit(){
        return Db::table(static::$configTable)->where(['EDIT_FLAG'=>'1'])->order('ID ASC')->select();
    }

    public static function getConfigPrint(){
        return Db::table(static::$configTable)->where(['PRINT_FLAG'=>'1'])->order('ID ASC')->select();
    }

    public static function exportExcel($data,$fileName){

        $fieldList = [
            ['title'=>'工程编号','field'=>'CONS_NO']
            ,['title'=>'项目名称','field'=>'CONS_NAME']
            ,['title'=>'计划投资总额','field'=>'TOTAL_FEE']
            ,['title'=>'累计完成投资','field'=>'ACC_FEE']
            ,['title'=>'开工日期','field'=>'START_DATE']
            ,['title'=>'计划竣工日期','field'=>'PLAN_COMPLETE_DATE']
            ,['title'=>'投入使用状况','field'=>'USE_STATE_NAME']
            ,['title'=>'计划投入使用日期','field'=>'USE_DATE']
            ,['title'=>'项目性质','field'=>'NATURE_NAME']
            ,['title'=>'项目类型','field'=>'TYPE_NAME']
            ,['title'=>'占地面积(㎡)','field'=>'BUILD_AREA']
            ,['title'=>'坐落位置','field'=>'LOCATION']
            ,['title'=>'制单人','field'=>'INPUT_USER']
            ,['title'=>'制单时间','field'=>'INPUT_TIME']
        ];

        $excel = new ExcelService();
        $excel->exportExcel($data,$fileName,$fieldList);
    }

   const CONSTRUCTION_FIELD_LIST = [
       "CONS_NO","CONS_NAME","TOTAL_FEE","ACC_FEE","START_DATE","PLAN_COMPLETE_DATE","USE_STATE_ID","USE_DATE","CONS_NATURE_ID","CONS_TYPE_ID","BUILD_AREA","LOCATION"
   ];

    public static function getSessionConditions() {
        $condition = null;
        $listCondition = Login::getListConditionConstruction();
        if (!empty($listCondition['CONS_NO'])) {
            $value = $listCondition['CONS_NO'];
            $condition[] = ['CONS_NO','like',"$value%"];
        }
        if (!empty($listCondition['CONS_NAME'])) {
            $value = $listCondition['CONS_NAME'];
            $condition[] = ['CONS_NAME','like',"%$value%"];
        }
        if (!empty($listCondition['ACC_FEE_START'])) {
            $value = $listCondition['ACC_FEE_START'];
            $condition[] = ['ACC_FEE','>=',$value];
        }
        if (!empty($listCondition['ACC_FEE_END'])) {
            $value = $listCondition['ACC_FEE_END'];
            $condition[] = ['ACC_FEE','<=',$value];
        }
        if (!empty($listCondition['USE_DATE_START'])) {
            $value = Db::raw("to_date('{$listCondition['USE_DATE_START']}','yyyy-mm-dd')");
            $condition[] = ['USE_DATE','>=',$value];
        }
        if (!empty($listCondition['USE_DATE_END'])) {
            $value = Db::raw("to_date('{$listCondition['USE_DATE_END']}','yyyy-mm-dd')");
            $condition[] = ['USE_DATE','<=',$value];
        }

        return $condition;
    }
}